n=int(input())
m=n
ca12=0
ca13=0
ca14=0
ca23=0
ca24=0
ca34=0
ca123=0
ca234=0
ca1234=0
maxx=0
ctus=0
csot=0
cdes=0
ced=0
tus=n//1000
if tus==1:
     ctus+=1
sot=(n%1000)//100
des=(n%100)//10
ed=(n%10)
for i in range(1,10):
     if tus%i==0:
          ctus+=1
     if sot%i==0:
          csot+=1
     if des%i==0:
          cdes+=1
     if ed%i==0:
          ced+=1
if ced<=2 and ed>maxx:
     maxx=ed
if cdes<=2 and des>maxx:
     maxx=des
if ctus<=2 and tus>maxx:
     maxx=tus
if csot<=2 and sot>maxx:
     maxx=sot
ed=str(ed)
des=str(des)
sot=str(sot)
tus=str(tus)
a1234=(tus[0]+sot[0]+des[0]+ed[0])
a12=(tus[0]+sot[0])
a23=(sot[0]+des[0])
a123=(tus[0]+sot[0]+des[0])
a234=(sot[0]+des[0]+ed[0])
a34=(tus[0]+tus[0])
a1234=int(a1234)
a12=int(a12)
a23=int(a23)
a34=int(a34)
a123=int(a123)
a234=int(a234)
for i in range(1,n):
     if a12%i==0:
          ca12+=1
     if a23%i==0:
          ca23+=1
     if a34%i==0:
          ca34+=1
     if a123%i==0:
          ca123+=1
     if a234%i==0:
          ca234+=1
     if a1234%i==0:
          ca1234+=1
if ca12<=2 and a12>maxx:
     maxx=a12
if ca23<=2 and a23>maxx:
     maxx=a23
if ca34<=2 and a34>maxx:
     maxx=a34
if ca123<=2 and a123>maxx:
     maxx=a123
if ca234<=2 and a234>maxx:
     maxx=a234
if ca1234<=2 and a1234>maxx:
     maxx=a1234








print(maxx)
